#coding=utf-8

from stockdb.misc import *
import strategy.basic as basic

def gap_filter(td, ratio=None):
    if not ratio:ratio=0
    
    pre = td.pre
    if not pre:return False
    
    lowest = pre.price.fq('lowest')
    highest = td.price.fq('highest')
    
    r = (lowest - highest) * 100/highest
    if r > ratio:
        #print '%.2f%%'%r
        return True
    
    return False
     
if __name__  == '__main__':
    
    dt = today
    dt = '2008-07-25'
    
    cs = get_stcodes()

    for c in cs:
        s = find_stock(c)
        
        if s.name.upper().find('S') != -1:
            continue
            
        day = s.find_td(dt)
        if not day:break
        
        tds = s.tds_since('2008-05-01')
        tf = basic.TDFilter(tds)
        tds = tf.filter_by(gap_filter, 2).all()
        
        now = day.price.fq('close')
        for td in tds:
            h = td.price.fq('highest')
            
            r = (h - now) * 100 / h 
            if r > -1 and r < 10:
                print s, td, h, now, r
            
